package com.jimmy.springcloud.controller;

import com.jimmy.springcloud.entities.Dept;
import com.jimmy.springcloud.service.IDepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @ ClassName DeptController
 * <p> TODO
 * @ author JIMMY
 * @ Date 2020/11/15 0015 下午 7:43
 */
@RestController
@RequestMapping("sys/dept")
public class DeptController {

    @Autowired
    private IDepService depService;

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept)
    {
        return depService.add(dept);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id)
    {
        return depService.get(id);
    }
    //是否是采用service的接口来做feign的调用故意取不同的方法名称
    @RequestMapping(value = "/get2/{id}", method = RequestMethod.GET)
    public Dept get2(@PathVariable("id") Long id)
    {
        return depService.getDepById(id);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Dept> list()
    {
        return depService.list();
    }


    /***
     * @Description  自测获取EURUKA服务治理中心的微服务信息 记得BOOT开启注解EnableDiscoveryClient
     * @author JIMMY   http://localhost:8001/sys/dept/discovery
     * @Date 2020/11/16 0016
     * @Param
     * @return
     */
    @RequestMapping(value = "/discovery", method = RequestMethod.GET)
    public Object discovery()
    {
        List<String> list = client.getServices();
        System.out.println("**********" + list);

        List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
        for (ServiceInstance element : srvList) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
                    + element.getUri());
        }
        return this.client;
    }


}
